package com.fu.dao;

import com.fu.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 付大大
 * @description
 * @create 2021-02-01 11:37
 */
public class StudentTest01 {

    @Test
    public void selectAll() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");

        for (Student student : list) {
            System.out.println(student);
        }
        sqlSession.close();
        resourceAsStream.close();
    }


    @Test
    public void selectById() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        Student student = sqlSession.selectOne("StudentMapper.selectById", 3);
        System.out.println(student);

        sqlSession.close();
        resourceAsStream.close();
    }

    @Test
    public void insert() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Student student = new Student(4, "赵六", 26);

        int count = sqlSession.insert("StudentMapper.insert", student);
        sqlSession.commit();
        System.out.println(count);
        sqlSession.close();
        resourceAsStream.close();
    }

    @Test
    public void update() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        Student student = new Student(4, "五五", 26);
        int count = sqlSession.update("StudentMapper.update", student);
        System.out.println(count);
        sqlSession.close();
        resourceAsStream.close();
    }



    @Test
    public void delete() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        int count = sqlSession.delete("StudentMapper.delete", 4);
        System.out.println(count);
        sqlSession.close();
        resourceAsStream.close();
    }
}
